<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>协议网关管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="searchFrom" class="search-form">
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="ID" width="150">
      </el-table-column>
      <el-table-column sortable prop="name" label="名称" width="150">
      </el-table-column>
      <el-table-column sortable prop="protocol" label="协议" width="100">
      </el-table-column>
      <el-table-column sortable prop="createAt" label="添加时间" width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.createAt | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            @click="handleEditScript(scope.$index, scope.row)"
            >配置脚本</el-button
          >
          <el-popconfirm
            title="确认要删除？"
            @confirm="handleDelete(scope.$index, scope.row)"
            class="mg-left-10"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:data="searchFrom"
      @onPagePaging="onPagePaging"
    ></Pagination>

    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="40%"
      @click="closeDialog"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="ID" prop="id">
          <el-input
            size="small"
            v-model="editForm.id"
            auto-complete="off"
            placeholder="请输入网关ID"
            :readonly="!isAdd"
          ></el-input>
        </el-form-item>
        <el-form-item label="网关名称" prop="name">
          <el-input
            size="small"
            v-model="editForm.name"
            auto-complete="off"
            placeholder="请输入网关名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="通讯协议" prop="protocol">
          <el-radio-group v-model="editForm.protocol">
            <el-radio label="mqtt">MQTT协议</el-radio>
            <el-radio label="coap">CoAP协议</el-radio>
            <el-radio label="tcp">TCP协议</el-radio>
            <el-radio label="http">HTTP协议</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitEditForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      title="脚本修改"
      :visible.sync="editScriptVisible"
      width="40%"
      @click="closeDialog"
    >
      <ScriptEditor></ScriptEditor>

      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="submitSaveScript"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  GatewayList,
  GatewayAdd,
  GatewaySave,
  GatewayDel,
  GatewayScriptSave,
} from "@/api/protocolApi";
import Pagination from "../../components/Pagination";
import ScriptEditor from "./ScriptEditor.vue";

export default {
  data() {
    var validateId = (rule, value, callback) => {
      console.log(new RegExp("^[\\-\\_a-z0-9]+$", "i").test(value));
      if (!new RegExp("^[\\-\\_a-z0-9]+$", "i").test(value)) {
        callback(new Error("id只能是数字、字母、-和_的组合"));
      } else {
        callback();
      }
    };

    return {
      loading: false,
      editFormVisible: false,
      editScriptVisible: false,
      isAdd: true,
      title: "",
      editForm: {
        id: "",
        name: "",
        protocol: "",
        config: 0,
      },
      rules: {
        id: [
          { required: true, message: "请输入网关ID", trigger: "blur" },
          { validator: validateId, trigger: "blur" },
        ],
      },
      searchFrom: {
        page: 1,
        size: 10,
        total: 0,
      },
      listData: [],
      editScript: {},
    };
  },
  components: {
    Pagination,
    ScriptEditor,
  },
  created() {
    this.getdata(this.searchFrom);
  },
  methods: {
    getdata(parameter) {
      this.loading = true;
      GatewayList(parameter).then((res) => {
        this.loading = false;
        this.listData = res.data;
        this.searchFrom.total = res.total;
      });
    },
    search() {
      this.getdata(this.searchFrom);
    },
    onPagePaging() {
      this.search();
    },
    handleEdit(index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.isAdd = false;
        this.title = "修改协议网关";
        this.editForm.id = row.id;
        this.editForm.name = row.name;
        this.editForm.protocol = row.protocol;
        this.editForm.config = row.config;
      } else {
        this.isAdd = true;
        this.title = "添加协议网关";
        this.editForm.id = "";
        this.editForm.name = "";
        this.editForm.protocol = "";
        this.editForm.config = "";
      }
    },
    handleEditScript(idx, row) {
      this.editScriptVisible = true;
      this.editScript.id = row.id;
      this.editScript.script = this.script;
    },
    handleDelete(idx, row) {
      this.loading = true;
      GatewayDel(row.id).then(() => {
        this.$notify({
          title: "成功",
          message: "删除成功",
          type: "success",
        });
        this.search();
      });
    },
    submitEditForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.isAdd) {
            GatewayAdd(this.editForm).then(() => {
              this.$notify({
                title: "成功",
                message: "添加成功",
                type: "success",
              });
              this.editFormVisible = false;
              this.loading = false;
              this.search();
            });
          } else {
            GatewaySave(this.editForm).then(() => {
              this.$notify({
                title: "成功",
                message: "修改成功",
                type: "success",
              });
              this.editFormVisible = false;
              this.loading = false;
              this.search();
            });
          }
        }
      });
    },
    submitSaveScript() {
      GatewayScriptSave(this.editScript).then(() => {
        this.$notify({
          title: "成功",
          message: "修改成功",
          type: "success",
        });
      });
    },
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>
